package com.lzp.systemopt

import android.app.Activity
import android.content.Intent
import android.net.Uri
import android.os.Bundle
import android.util.Log
import android.view.View
import com.base.BaseActivity
import com.imageloader.ImageLoader
import com.imageloader.interfaces.ImgLoadListener
import com.utils.LogUtils
import com.utils.OsUtils
import com.utils.PathUtils
import com.utils.ToastUtils
import com.utils.permission.PermissionConstant
import com.utils.permission.PermissionListener
import com.utils.permission.PermissionUtils
import kotlinx.android.synthetic.main.image_utils_activity.*
import java.io.File
import java.lang.Exception

/**
 * @describe:
 * @Author: lixiaopeng
 * @Date: 2020/11/11
 */
class ImageUtilsActivity : BaseActivity(), View.OnClickListener {

    private val TAG = "ImageUtilsActivity"
    private var imagePath = ""
    private var cropPath = ""
    private var choseUri: Uri? = null

    private var isNet = false

    private val netImg = "https://goss.veer.com/creative/vcg/veer/1600water/veer-375318408.jpg"


    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.image_utils_activity)
    }

    override fun initView() {
        super.initView()
        val permissions = arrayOf(PermissionConstant.CAMERA, PermissionConstant.READ_EXTERNAL_STORAGE, PermissionConstant.WRITE_EXTERNAL_STORAGE)
        if (!PermissionUtils.hasPermissions(this, permissions)) {
            PermissionUtils.getPermission(this, permissions, object : PermissionListener {
                override fun onDenied(deniedPermissions: MutableList<String>?, neverTips: Boolean) {
                    ToastUtils.toastText(applicationContext, "您拒绝了相关权限")
                    onBackPressed()
                }
            })
            return
        }
    }

    override fun setListener() {
        super.setListener()
        cameraTv.setOnClickListener(this)
        pickTv.setOnClickListener(this)
        cropTv.setOnClickListener(this)
        netTv.setOnClickListener(this)
    }

    override fun onClick(v: View?) {
        when (v?.id) {
            R.id.cameraTv -> {
                isNet = false
                imagePath = PathUtils.randomCacheImage()
                OsUtils.startSystemCamera(this, imagePath)
            }
            R.id.pickTv -> {
                isNet = false
                OsUtils.startImagePicker(this)
            }

            R.id.netTv->{
                isNet = true
                ImageLoader.get(this).display(netImg).into(iv)
            }
            R.id.cropTv -> {
                if (isNet){
                    //保存到本地，方便裁剪
                    ImageLoader.get(this).load(netImg).listener(object :ImgLoadListener.LoadListener{
                        override fun fail(e: Exception?) {
                            LogUtils.d(TAG, "fail: 图片保存失败")
                            ToastUtils.toastText(applicationContext,"图片保存失败")
                        }

                        override fun success(file: File?) {
                            if (file==null||!file.exists()){
                                ToastUtils.toastText(applicationContext,"图片保存失败")
                                return
                            }
                            LogUtils.d(TAG, "${file.absoluteFile}")
                            startCrop(PathUtils.fileToUri(file))
                        }
                    }).start()
                    return
                }
                if (choseUri == null) {
                    ToastUtils.toastText(this, "请先选择一张图片")
                    return
                }
                choseUri?.let {
                    startCrop(it)
                }
            }
        }
    }

    private fun startCrop(choseUri: Uri){
        cropPath = PathUtils.randomCacheImage()
        OsUtils.startPictureCrop(this, choseUri, cropPath, 480, 480)
    }

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        Log.d(TAG, "onActivityResult: $resultCode")
        super.onActivityResult(requestCode, resultCode, data)
        if (isFinishing || resultCode != Activity.RESULT_OK) {
            return
        }
        if (requestCode == OsUtils.REQUEST_CODE_CAMERA) {//拍照
            Log.d(TAG, "onActivityResult: $imagePath")
            choseUri = PathUtils.fileToUri(File(imagePath))
            ImageLoader.get(this).display(choseUri.toString()).into(iv)
        } else if (requestCode == OsUtils.REQUEST_CODE_CROP) {
            Log.d(TAG, "onActivityResult: $cropPath")
            ImageLoader.get(this).display(PathUtils.fileToUri(File(cropPath)).toString()).into(iv)
        } else if (requestCode == OsUtils.REQUEST_CODE_PICK_IMAGE) {
            val imageUri = data?.data
            imageUri?.let {
                Log.d(TAG, "onActivityResult: $it")
                choseUri = it
                ImageLoader.get(this).display(it.toString()).into(iv)
            }
        }
    }
}